package com.alm.token.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.MyApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.RegisterData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.BaseView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.LanguageUtils;
import com.alm.token.utils.SpUtil;

import org.web3j.crypto.Keys;

import java.util.ArrayList;
import java.util.List;

import baibeiwallet.Constant;
import baibeiwallet.WalletManager;
import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetLanguageActivity extends BaseActivity implements BaseView<RegisterData> {


    @BindView(R.id.img_chineseclick)
    ImageView imgChineseclick;
    @BindView(R.id.chinese_item)
    RelativeLayout chineseItem;
    @BindView(R.id.img_englishclick)
    ImageView imgEnglishclick;
    @BindView(R.id.english_item)
    RelativeLayout englishItem;
    @BindView(R.id.hanwen_item)
    RelativeLayout hanwenItem;
    @BindView(R.id.japanese_item)
    RelativeLayout japaneseItem;
    @BindView(R.id.france_item)
    RelativeLayout franceItem;
    @BindView(R.id.ly_languagebody)
    LinearLayout lyLanguagebody;
    @BindView(R.id.tv_contractaddress)
    TextView tvContractaddress;
    @BindView(R.id.ly_contractaddressbody)
    LinearLayout lyContractaddressbody;
    @BindView(R.id.img_hanwenclick)
    ImageView imgHanwenclick;
    @BindView(R.id.img_japaneseclick)
    ImageView imgJapaneseclick;
    @BindView(R.id.img_franceclick)
    ImageView imgFranceclick;
    private int type = 0;

    private List<ImageView> imgitemclicks = new ArrayList<>();
    private final String LABGUAGE_ZH = "zh_CN", LABGUAGE_EN = "en_US", LABGUAGE_KO = "ko_KR", LABGUAGE_JA = "ja_JP", LABGUAGE_FR = "fr_FR";

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_setlanguage;
    }

    @Override
    protected void initVariables() {
        type = getIntent().getIntExtra("type", 0);
    }


    @Override
    protected void initViews() {
        if (type == 0) {
            new ActionBarHelper(this).setTitle(getString(R.string.title_hint_item8));
            lyLanguagebody.setVisibility(View.VISIBLE);
            lyContractaddressbody.setVisibility(View.GONE);
            imgitemclicks.add(imgChineseclick);
            imgitemclicks.add(imgEnglishclick);
            imgitemclicks.add(imgHanwenclick);
            imgitemclicks.add(imgJapaneseclick);
            imgitemclicks.add(imgFranceclick);

            int index = LanguageUtils.getSetIndex();
            if (index != 0) {
                for (ImageView imageView : imgitemclicks) {
                    imageView.setVisibility(View.GONE);
                }
                imgitemclicks.get(index - 1).setVisibility(View.VISIBLE);
            }
        } else if (type == 1) {
            new ActionBarHelper(this).setTitle(getString(R.string.title_hint_item9));
            lyLanguagebody.setVisibility(View.GONE);
            lyContractaddressbody.setVisibility(View.VISIBLE);

            tvContractaddress.setText(Keys.toChecksumAddress(Constant.G12ContractAddress));
        }
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {

    }

    @OnClick({R.id.chinese_item, R.id.english_item, R.id.hanwen_item, R.id.japanese_item, R.id.france_item})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.chinese_item:
                if (imgChineseclick.isShown()) {
                    break;
                }
                UserWalletData.DataBean.SysMsgBean sysMsgBean = new UserWalletData.DataBean.SysMsgBean();
                sysMsgBean.setTitle("语言设置提示");
                sysMsgBean.setContent("是否设置语言:简体中文!");
                sysMsgBean.setUpStartTimeStr("取消");
                sysMsgBean.setUpEndTimeStr("确定");
                DialogUtils.showInforsetLang(this, sysMsgBean, (pwd) -> {
                    preenter.setLangType(LABGUAGE_ZH);
                });


                break;
            case R.id.english_item:

                if (imgEnglishclick.isShown()) {
                    break;
                }
                UserWalletData.DataBean.SysMsgBean sysMsgBean1 = new UserWalletData.DataBean.SysMsgBean();
                sysMsgBean1.setTitle("Language setting tips");
                sysMsgBean1.setContent("Whether to set the language:English!");
                sysMsgBean1.setUpStartTimeStr("Cancel");
                sysMsgBean1.setUpEndTimeStr("Confirm");
                DialogUtils.showInforsetLang(this, sysMsgBean1, (pwd) -> {
                    preenter.setLangType(LABGUAGE_EN);
                });

                break;
            case R.id.hanwen_item:
                if (imgHanwenclick.isShown()) {
                    break;
                }
                UserWalletData.DataBean.SysMsgBean sysMsgBean2 = new UserWalletData.DataBean.SysMsgBean();
                sysMsgBean2.setTitle("언어 설정 팁");
                sysMsgBean2.setContent("언어 설정 여부:한국어!");
                sysMsgBean2.setUpStartTimeStr("취소하다");
                sysMsgBean2.setUpEndTimeStr("확정하다");
                DialogUtils.showInforsetLang(this, sysMsgBean2, (pwd) -> {
                    preenter.setLangType(LABGUAGE_KO);
                });
                break;
            case R.id.japanese_item:
                if (imgJapaneseclick.isShown()) {
                    break;
                }
                UserWalletData.DataBean.SysMsgBean sysMsgBean3 = new UserWalletData.DataBean.SysMsgBean();
                sysMsgBean3.setTitle("言語設定のヒント");
                sysMsgBean3.setContent("言語を設定するか否か:日本語!");
                sysMsgBean3.setUpStartTimeStr("やめる");
                sysMsgBean3.setUpEndTimeStr("かくてい");
                DialogUtils.showInforsetLang(this, sysMsgBean3, (pwd) -> {
                    preenter.setLangType(LABGUAGE_JA);
                });
//                for (ImageView imageView : imgitemclicks) {
//                    imageView.setVisibility(View.GONE);
//                }
//                imgJapaneseclick.setVisibility(View.VISIBLE);
//                SpUtil.putString(MyApp.context, ConStant.LANGAUE, ""+ LanguageUtils.JAPANESE);
                break;
            case R.id.france_item:
//                if(ConStant.ISDEBUG){
//                    if (imgFranceclick.isShown()) {
//                        break;
//                    }
//                    UserWalletData.DataBean.SysMsgBean sysMsgBean2 = new UserWalletData.DataBean.SysMsgBean();
//                    sysMsgBean2.setTitle("语言设置提示");
//                    sysMsgBean2.setContent("是否设置语言:法文!");
//                    sysMsgBean2.setUpStartTimeStr("取消");
//                    sysMsgBean2.setUpEndTimeStr("确定");
//                    DialogUtils.showInforsetLang(this, sysMsgBean2, (pwd) -> {
//                        preenter.setLangType(LABGUAGE_FR);
//                    });
//                }else {
                showToast(getString(R.string.set_hint_warn));
//                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    public void refreshLangaue() {
        MyApp.userName = "";
        WalletManager.getInstance().mListWallets = new ArrayList<>();
        Intent intent = new Intent();
        intent.setAction(ConStant.EXIT_APP);
        sendBroadcast(intent);
        startActivity(SplashActivity.class);
        finish();
    }

    @Override
    public void getData(RegisterData registerData) {
        if (registerData.getStatus() == 0) {
            for (ImageView imageView : imgitemclicks) {
                imageView.setVisibility(View.GONE);
            }
            if (registerData.getData().indexOf(LABGUAGE_ZH) != -1) {
                imgChineseclick.setVisibility(View.VISIBLE);
                SpUtil.putString(MyApp.context, ConStant.LANGAUE, "" + LanguageUtils.CHINESE_SIMPLE);
                SpUtil.putString(MyApp.context, ConStant.COOKIELANGAUE, "" + LanguageUtils.COOKIE_CHINESE);
            } else if (registerData.getData().indexOf(LABGUAGE_EN) != -1) {
                imgEnglishclick.setVisibility(View.VISIBLE);
                SpUtil.putString(MyApp.context, ConStant.LANGAUE, "" + LanguageUtils.ENGLISH);
                SpUtil.putString(MyApp.context, ConStant.COOKIELANGAUE, "" + LanguageUtils.COOKIE_ENGLISH);
            } else if (registerData.getData().indexOf(LABGUAGE_KO) != -1) {
                imgHanwenclick.setVisibility(View.VISIBLE);
                SpUtil.putString(MyApp.context, ConStant.LANGAUE, "" + LanguageUtils.KOREAN);
                SpUtil.putString(MyApp.context, ConStant.COOKIELANGAUE, "" + LanguageUtils.COOKIE_KOREAN);
            } else if (registerData.getData().indexOf(LABGUAGE_JA) != -1) {
                imgJapaneseclick.setVisibility(View.VISIBLE);
                SpUtil.putString(MyApp.context, ConStant.LANGAUE, "" + LanguageUtils.JAPANESE);
                SpUtil.putString(MyApp.context, ConStant.COOKIELANGAUE, "" + LanguageUtils.COOKIE_JAPANESE);
            } else if (registerData.getData().indexOf(LABGUAGE_FR) != -1) {
                imgFranceclick.setVisibility(View.VISIBLE);
                SpUtil.putString(MyApp.context, ConStant.LANGAUE, "" + LanguageUtils.FRANCE);
                SpUtil.putString(MyApp.context, ConStant.COOKIELANGAUE, "" + LanguageUtils.COOKIE_FRANCE);
            }

            refreshLangaue();
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }
}
